<?php

class helper_mysqli
{

    public static $_MYSQLI;
    public static $_QUEUE;
	/**
	 * @brief main class 
	 **/
	public static function doConnect( $sHostName, $sUserName, $sPassword, $sDatabaseName )
	{
        if(is_null(self::$_MYSQLI)){
            self::$_MYSQLI = new mysqli($sHostName, $sUserName, $sPassword, $sDatabaseName);
        }

        return self::$_MYSQLI;	
    }
	
	function __destruct()
	{
		$queries = self::$_QUEUE;

		if ( is_array( self::$_QUEUE ) ) {
			$queries = implode(";", self::$_QUEUE);
		}

		if ( ! empty( $queries ) ) {
			//$oResult = self::$_MYSQLI -> query($queries);
		}
	}
	
	public function doExecute( $query, $autocommit = 1 )
	{
       self::$_QUEUE[] = $query;  
       $oResult = self::$_MYSQLI -> query($query);

       if(isset(self::$_MYSQLI->error['errno'])){
            helper('error')->doPrintQueryError(self::$_MYSQLI);
       }
       //return $oResult;
	}

	/**
	 * @brief Get row by fetch_array 
     * @params $query   | string    | sql query 
     * @return $aRows   | array     | result record data
	 **/
	public function doQuery( $query, $autocommit = 1 )
	{
       $oResult = self::$_MYSQLI -> query($query);

       if(isset(self::$_MYSQLI->error['errno'])){
            helper('error')->doPrintQueryError(self::$_MYSQLI);
       }
       return $oResult;
	}

	/**
	 * @brief Get row by fetch_array 
     * @params $query   | string    | sql query 
     * @return $aRows   | array     | result record data
	 **/
	public function getFetchArray( $query)
	{
       $oResult = self::doQuery($query);

       $temp =array();
       while($oRow = $oResult->fetch_array(MYSQLI_BOTH)){
          $aTemp[] = $oRow; 
       }

       return $aTemp;
	}

	/**
	 * @brief Get row by fetch_array 
     * @params $query   | string    | sql query 
     * @return $aRows   | array     | result record data by indexing array
	 **/
	public function getFetchAssoc( $query)
	{
       $oResult = self::doQuery($query);

       $temp =array();
       while($oRow = $oResult->fetch_array(MYSQLI_ASSOC)){
          $aTemp[] = $oRow; 
       }
       return $aTemp;
	}

	/**
	 * @brief Get row by fetch_array 
     * @params $query   | string    | sql query 
     * @return $aRows   | array     | result record data linked array
	 **/
	public function getFetchRow( $query)
	{
       $oResult = self::doQuery($query);

       $temp =array();
       while($oRow = $oResult->fetch_array(MYSQLI_NUM)){
          $aTemp[] = $oRow; 
       }
       return $aTemp;
	}

	/**
	 * @brief Get only once row by fetch_assoc 
     * @params $query   | string    | sql query 
     * @return $aRows   | array     | result record data by indexing array
	 **/
	public function getFetchOne( $query)
	{
       $oResult = self::doQuery($query);
       $oRow = $oResult->fetch_array(MYSQLI_ASSOC);
       return $oRow;
	}

}
	
